package com.ambimmort.ucagent.modules.server.channels;

import org.apache.mina.core.session.IoSession;
import com.ambimmort.ucagent.modules.server.dpi.DPIEndPoint;

/**
 * @author 何定巍
 */
public class ServerChannel implements IChannel {

    private DPIEndPoint dpiEndPoint = null;

    private IoSession session = null;

    public ServerChannel(DPIEndPoint dpiEndPoint) {
        this.dpiEndPoint = dpiEndPoint;
    }

    /*
     * (non-Javadoc)
     * 
     * @see uc.agent.component.ucserver.transport.channel.IChannel#disconnect()
     */
    @Override
    public void disconnect() {
        if (session != null) {
            session.close(true);
            dpiEndPoint.getDpiEndPointBean().setIsServerChannelConnected(false);
            dpiEndPoint.persistence();
        }
    }

    /*
     * (non-Javadoc)
     * 
     * @see uc.agent.component.ucserver.transport.channel.IChannel#getSession()
     */
    @Override
    public IoSession getSession() {
        return session;
    }

    public void setSession(IoSession session) {
        this.session = session;
    }

    /*
     * (non-Javadoc)
     * 
     * @see
     * uc.agent.component.ucserver.transport.channel.IChannel#getDpiEndPoint()
     */
    @Override
    public DPIEndPoint getDpiEndPoint() {
        return dpiEndPoint;
    }

    /*
     * (non-Javadoc)
     * 
     * @see uc.agent.component.ucserver.transport.channel.IChannel#isConnected()
     */
    @Override
    public boolean isConnected() {
        if (session != null) {
            return session.isConnected();
        } else {
            return false;
        }
    }

}
